#include "formlcd.h"
#include "ui_formlcd.h"
#include <QRectF>
#include <QPainter>
#include <QDebug>
#include <QThread>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>

#include "UGUI/ugui.h"
#include "LCD/lcd.h"

extern "C" void lv_tutorial_hello_world(void);

#define  COLOR_RGB_RED       0xFFFF0000
#define  COLOR_RGB_GREEN     0xFF00FF00
#define  COLOR_RGB_YELLOW    0xFFFFFF00
#define  COLOR_RGB_BLUE      0xFF0000FF
//#define  COLOR_RGB_PURPLE    0xFFA020F0
#define  COLOR_RGB_PURPLE    0xFFFF00FF
#define  COLOR_RGB_CYAN      0xFF00FFFF
#define  COLOR_RGB_WHITE     0xFFFFFFFF
#define  COLOR_RGB_BLACK     0x00000000

static inline uint32_t rgb_mixture(const uint32_t color, const uint32_t next_color, const uint8_t mixture1)
{
    //int i;
    uint32_t rgb1=0, rgb2=0;
    uint32_t color1, color2;
    rgb2 = 0xFF000000;
    for(int i=0; i<24; i+=8)
    {
        color1 = (color>>i)&0xFF;
        color2 = (next_color>>i)&0xFF;
        //rgb1 = (color1*(255-mixture)+color2*mixture)>>8;
        rgb1 = (color2*(255-mixture1)+color1*mixture1+255)>>8;
        //rgb1 = (color1>>mixture)&0xFF+ (color2<<(8-mixture))&0xFF;
        rgb2 |= (rgb1<<i);
    }
    return rgb2;
}
static int color_to_color2(const uint8_t scale[], const uint8_t scale_len, int16_t &_grayscale, uint8_t &_mode, const int16_t gmin, const int16_t gmax, const uint8_t step, uint32_t &color, const uint32_t color2)
{
    uint32_t color_tmp=0;
    uint16_t red,green,blue;
    int ret = -1;
#if 0
    if(0==_mode) // color2 ++
    {
        //qDebug("[%s--%d] color[%08X %08X %08X]", __func__, __LINE__, color, color2, color_tmp, _grayscale);
        color_tmp = rgb_mixture(color2, 0x00000000, _grayscale);
        //qDebug("[%s--%d] color[%08X %08X %08X] gray:%3d", __func__, __LINE__, color, color2, color_tmp, _grayscale);
        _grayscale += step;
        if(_grayscale>=gmax) { _mode=1; _grayscale=gmax;}
        red = ((color_tmp>>16)&0xFF) + ((color>>16)&0xFF);
        green = ((color_tmp>>8)&0xFF) + ((color>>8)&0xFF);
        blue = ((color_tmp>>0)&0xFF) + ((color>>0)&0xFF);
        color_tmp = (color>>24)&0xFF;
        //qDebug("[%s--%d] color[%08X %08X]", __func__, __LINE__, color,  color_tmp);
        color_tmp = (color_tmp<<8) + (red&0xFF);
        //qDebug("[%s--%d] color[%08X %08X]", __func__, __LINE__, red,  color_tmp);
        color_tmp = (color_tmp<<8) + (green&0xFF);
        //qDebug("[%s--%d] color[%08X %08X]", __func__, __LINE__, green,  color_tmp);
        color_tmp = (color_tmp<<8) + (blue&0xFF);
        //qDebug("[%s--%d] color[%08X %08X]", __func__, __LINE__, blue,  color_tmp);
        //qDebug("[%s--%d] color[%08X %08X %08X] rgb[%04X %04X %04X] _grayscale[%3d] \r\n", __func__, __LINE__, color, color2, color_tmp, red, green, blue, _grayscale);
        color = color_tmp;
    }
    else // color --
    {
        color_tmp = rgb_mixture(color, 0x00000000, _grayscale);
        _grayscale -= step;
        if(_grayscale<=gmin) { _mode=0; _grayscale=gmin; ret = 0; }
        red = ((color_tmp>>16)&0xFF) + ((color2>>16)&0xFF);
        green = ((color_tmp>>8)&0xFF) + ((color2>>8)&0xFF);
        blue = ((color_tmp>>0)&0xFF) + ((color2>>0)&0xFF);
        color_tmp = (color>>24)&0xFF;
        color_tmp = (color_tmp<<8) + (red&0xFF);
        color_tmp = (color_tmp<<8) + (green&0xFF);
        color_tmp = (color_tmp<<8) + (blue&0xFF);
        color = color_tmp;
    }
#else
    //const uint8_t scale[] = {230, 204, 180, 153, 130, 102, 80, 51, 30, 0};
    //const uint8_t scale[] = {0, 45, 85, 170, 210, 255};
    //const uint8_t scale_len = sizeof(scale);
    if(0==_mode) // color2 ++
    {
        //qDebug("[%s--%d] color[%08X %08X %08X]", __func__, __LINE__, color, color2, color_tmp, _grayscale);
        if(_grayscale>=scale_len) _grayscale=scale_len-1;
        if(_grayscale<0) _grayscale=0;
        color_tmp = rgb_mixture(color2, 0x00000000, scale[_grayscale]);
        //qDebug("[%s--%d] color[%08X %08X %08X] gray:%3d", __func__, __LINE__, color, color2, color_tmp, _grayscale);
        _grayscale++;
        if(_grayscale>=(scale_len-1)) { _mode=1; _grayscale=scale_len-1;}
        red = ((color_tmp>>16)&0xFF) + ((color>>16)&0xFF);
        green = ((color_tmp>>8)&0xFF) + ((color>>8)&0xFF);
        blue = ((color_tmp>>0)&0xFF) + ((color>>0)&0xFF);
        color_tmp = (color>>24)&0xFF;
        //qDebug("[%s--%d] color[%08X %08X]", __func__, __LINE__, color,  color_tmp);
        color_tmp = (color_tmp<<8) + (red&0xFF);
        //qDebug("[%s--%d] color[%08X %08X]", __func__, __LINE__, red,  color_tmp);
        color_tmp = (color_tmp<<8) + (green&0xFF);
        //qDebug("[%s--%d] color[%08X %08X]", __func__, __LINE__, green,  color_tmp);
        color_tmp = (color_tmp<<8) + (blue&0xFF);
        //qDebug("[%s--%d] color[%08X %08X]", __func__, __LINE__, blue,  color_tmp);
        //qDebug("[%s--%d] color[%08X %08X %08X] rgb[%04X %04X %04X] _grayscale[%3d] \r\n", __func__, __LINE__, color, color2, color_tmp, red, green, blue, _grayscale);
        color = color_tmp;
    }
    else // color --
    {
        if(_grayscale>=scale_len) _grayscale=scale_len-1;
        if(_grayscale<0) _grayscale=0;
        color_tmp = rgb_mixture(color, 0x00000000, scale[_grayscale]);
        _grayscale --;
        if(_grayscale<=0) { _mode=0; _grayscale=0; ret = 0; }
        red = ((color_tmp>>16)&0xFF) + ((color2>>16)&0xFF);
        green = ((color_tmp>>8)&0xFF) + ((color2>>8)&0xFF);
        blue = ((color_tmp>>0)&0xFF) + ((color2>>0)&0xFF);
        color_tmp = (color>>24)&0xFF;
        color_tmp = (color_tmp<<8) + (red&0xFF);
        color_tmp = (color_tmp<<8) + (green&0xFF);
        color_tmp = (color_tmp<<8) + (blue&0xFF);
        color = color_tmp;
    }
#endif
    return ret;
}
void Bar_Palette_Init(const uint8_t scale[], const uint8_t scale_len, uint32_t bar[128], int16_t &bar_len)
{
    const uint32_t Bar_Palette[3] = {COLOR_RGB_RED, COLOR_RGB_GREEN, COLOR_RGB_BLUE};
    const int16_t Bar_Len = 3;
//    const uint8_t scale[] = {0, 45, 85, 170, 210, 255};
//    const uint8_t scale_len = sizeof(scale);
    int index;
    uint32_t color, color2;
    int16_t cIndex, _cIndex;
    int16_t gray2=0;
    uint8_t gray2_mode=0;
    cIndex=0;
    //_cIndex=0;
    gray2 = 0;//gray;
    gray2_mode = 0;//gray_mode;
    bar_len =48;
    for(index=0; index<128; index++)
    {
        color = Bar_Palette[cIndex];
        _cIndex=cIndex+1;
        if(_cIndex>=Bar_Len) _cIndex=0;
        if(_cIndex<0) _cIndex=Bar_Len-1;
        color2 = Bar_Palette[_cIndex];
        //color = rgb_mixture(color, color2, scale[gray2]);
        //color = rgb_mixture(color, color2, gray2);
        //color = rgb_mixture(color2, color, gray2);
        //color = rgb2grb(color, Draw.Grayscale);
        //bar[index] = color;
        //qDebug("[%s--%d] Palette[%08X %08X] bar[%3d]:%08X, scale[%2d]:%3d", __func__, __LINE__, Palette[cIndex]&0xFFFFFFFF, Palette[_cIndex]&0xFFFFFFFF, index, bar[index], gray2, scale[gray2]);
        //qDebug("[%s--%d] Palette[%08X %08X] bar[%3d]:%08X, scale[%2d %3d]", __func__, __LINE__, Palette[cIndex]&0xFFFFFFFF, Palette[_cIndex]&0xFFFFFFFF, index, bar[index], gray, gray2);
        //gray2++;
        //if(sizeof(scale)<=gray2)
        //if(0==grayGradual(gray2, gray2_mode, 0, 255, 32))
        if(0==color_to_color2(scale, scale_len, gray2, gray2_mode, 0, 255, 32, color, color2))
        {
            //qDebug("[%s--%d] Palette[%08X %08X] bar[%3d]:%08X, scale[%2d %3d]", __func__, __LINE__, Palette[cIndex]&0xFFFFFFFF, Palette[_cIndex]&0xFFFFFFFF, index, color, gray2_mode, gray2);
            //gray2=1;
            cIndex++;
            if(cIndex>=Bar_Len) cIndex=0;
            if(cIndex<0) cIndex=Bar_Len-1;
        }
        //else qDebug("[%s--%d] Palette[%08X %08X] bar[%3d]:%08X, scale[%2d %3d]", __func__, __LINE__, Palette[cIndex]&0xFFFFFFFF, Palette[_cIndex]&0xFFFFFFFF, index, color, gray2_mode, gray2);
        if((0!=index) && (color==bar[0]))
        {
            bar_len = index;
            break;
        }
        bar[index] = color;
    }
}


//static const uint8_t scale[] = {0, 64, 128, 192, 255};
static const uint8_t scale[] = {0, 64, 160, 255};
//static const uint8_t scale[] = {0, 170, 255};
static uint32_t bar[128] = {0xFF0000, };
static int16_t bar_len;
FormLCD::FormLCD(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormLCD),
    Diagram(this),
    Controller(0,0, MATRIX_SIZE, MATRIX_SIZE),
    rgbThread(Controller)
{
    ui->setupUi(this);
    //setFixedSize(this->width()+370, this->height()+370);
    setFixedSize(LCD_WIDTH*LCD_PIX, LCD_HEIGHT*LCD_PIX);
    adjustSize();

    ugui_init();
    Bar_Palette_Init(scale, sizeof(scale), bar, bar_len);
    UG_DrawLine(0, 0, 200, 200, bar[0]);
    //UG_FillCircle( 100, 100, 50, bar[5]);
    UG_PutString( 20, 20, "Hello");
    UG_DrawCircle(20, 21, 20, COLOR_RGB_GREEN);
    UG_DrawCircle(20, 41, 20, COLOR_RGB_GREEN);
    /*lv_init();
    lv_port_disp_init();
    lv_tutorial_hello_world();*/

    // 画正弦函数曲线
    // UG_DrawPixel( UG_S16 x0, UG_S16 y0, UG_COLOR c );
    Angle = 0.0f;
    //draw_sina(Angle, 100);

    m_time = new QTimer();
    connect(m_time,&QTimer::timeout,this,&FormLCD::valueChange);
    m_time->start(20);
#if 0
    printf("cross1[] = {\r\n");
    int cnt = 0;
    int move=0;
    for(int index=0; index<6; index++)
    {
        cnt = move;
        printf("{");
        for(int i=0; i<21; i++)
        {
            printf("%d, ", cnt/3);
            cnt++;
            if(cnt>=9) cnt=0;
        }
        printf("},\r\n");
        move++;
    }
    printf("};\r\n");
    printf("cross2[] = {\r\n");
    move=8;
    for(int index=0; index<6; index++)
    {
        cnt = move;
        printf("{");
        for(int i=0; i<21; i++)
        {
            printf("%d, ", cnt/3);
            cnt++;
            if(cnt>=9) cnt=0;
        }
        printf("},\r\n");
        move--;
    }
    printf("};\r\n");
#endif
    //Diagram.get_draw_square_map();
    ugui_init();
    connect(&rgbThread, &RGB_Thread::finished, &rgbThread, &QObject::deleteLater);
    rgbThread.start();
    rgbThread.set_run();
}

FormLCD::~FormLCD()
{
    m_time->stop();
    rgbThread.StopThread();
    QThread::msleep(100);
    disconnect(m_time,&QTimer::timeout,this,&FormLCD::valueChange);
    rgbThread.quit();
    rgbThread.wait();
    disconnect(&rgbThread, &RGB_Thread::finished, &rgbThread, &QObject::deleteLater);
    delete m_time;
    delete ui;
}

/**
https://www.cnblogs.com/lifexy/p/9203929.html
https://www.bbsmax.com/A/mo5kqy9Wzw/
https://www.freesion.com/article/5082156934/
*/
void FormLCD::paintEvent(QPaintEvent * event)
{
    QWidget::paintEvent(event);
    static int cnt=0;
    if(cnt++>=(bar_len-1)) cnt=0;
    //UG_FillCircle( 100, 100, 50, bar[cnt++]);
    //draw_sina(Angle, 100);
#if 0
#if 1
    // 画网格
    //gridding(LCD_WIDTH, LCD_HEIGHT, LCD_PIX);
    //gridding(width(), height(), 120, 120);
    //gridding(width(), height(), 100, 10);
    //draw_square(width(), height(), 41, 18);

    // 画等分圆
    //draw_circle(width(), height(), 18);
    //Diagram.divide_circle(18);
    Diagram.square3(41, 24);
    //Diagram.circle(MATRIX_SIZE, 24);
    // 画网格
    //Diagram.gridding(6, 21);
    //Diagram.square(21);

    // 画等分圆
    //Diagram.divide_arc(24);
#else
    QPainter paint(this);
    paint.setPen(QPen(Qt::green,2));


    //画左边大矩形框
    paint.drawRect(150,100,50,25);
    //画右边小矩形框
    paint.drawRect(200,107,5,10);

    paint.setBrush(Qt::green);

    //填充左边大矩形框
    paint.drawRect(150,100,m_value,25);

    //当充满时候填充右边小矩形框
    if(m_value == 50)
    {
        paint.drawRect(200,107,5,10);
    }
#endif
#else
    // 刷新界面
    simulation(MATRIX_SIZE, MATRIX_SIZE);
    //simulation(6, 21);
#endif
    Bar_Cnt = cnt;
}

void FormLCD::draw_sina(float &Angle, const uint8_t num)
{
    int i;
    const uint16_t divide = 20;
    // 画正弦函数曲线
    // UG_DrawPixel( UG_S16 x0, UG_S16 y0, UG_COLOR c );
    const float pi = 3.1415926;
    const float circum = 3.1415926f*2;
    const float Degree = 2*pi/divide + 0.01745f;   // pi/180=0.01745
    //const float Degree = 2*pi/divide + 0.01f;   // pi/180=0.01745
    //float degree;
    float angle = 2*pi/divide;
    float radii = 100/2;
    //int Radii = radii/2;
    float py=0.0f;
    //float px=0.0f;
    //float ph=0.0f;
    //float pw=0.0f;
    angle = Angle;
    UG_FillScreen( C_BLACK );
    for(i=num; i>=0; i--)//for(i=0; i<num; i++)//for(degree=0.0f; degree<Degree; degree +=0.01745f )
    {
        // 根据三角函数计算出以圆心为原点的等分圆的x,y坐标
        //angle = (circum*0)/divide+degree;
        py = qSin(angle)*radii;
        //py = qCos(angle)*radii;
        //px = qCos(angle)*radii;
        //pw += 1.0f;
        //ph += 1.0f;
        //qDebug("[%s--%d] angle:%f, \t%f \tph:%d", __func__, __LINE__, angle, py, i);
        UG_DrawPixel( 100+i, py+100, 0x00FF0000 );
        UG_DrawPixel( 100-i, py+100, 0x00FF0000 );
        angle += Degree;//0.01745f*15;
        //angle -= Degree;//0.01745f*15;
        if(angle>circum) angle -= circum;
        if(angle<(-circum)) angle += circum;
    }
    Angle += Degree;//0.01745f*15;
    //Angle -= Degree;
    if(Angle>circum) Angle -= circum;
    if(Angle<(-circum)) Angle += circum;
}
/**
(0,0)
-------->x+
\
\
\
y-
*/
void FormLCD::gridding(const int _width, const int _height, const uint16_t pix_size)
{
    if(_width<1) return;
    if(_height<1) return;
    const int Width = _width*pix_size;
    const int Height = _height*pix_size;
    //qDebug("[%s--%d] _width:[%d %d] _high:[%d %d]", __func__, __LINE__, _width, Width, _height, Height);
    int i;
    QPainter painter(this);
    //painter.setPen(QPen(QColor(11,67,127),8));
    painter.setPen(QPen(QColor(255,255,255),8));
    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿
    //QSize ViewWH(100,100);        //定义视口宽高
    QSize ViewWH(Width, Height);        //定义视口宽高
    painter.setViewport(0, 0,ViewWH.width(),ViewWH.height());
    //painter.setWindow(-50,50,ViewWH.width(),-ViewWH.height());
    painter.setWindow(0,0,ViewWH.width(),-ViewWH.height());
    painter.setBrush(QColor(187,229,253));
    //painter.setPen(QPen(QColor(11,67,127),2));
    painter.setPen(QPen(QColor(255,255,255),1));
    painter.drawRect(0,0,ViewWH.width(),-ViewWH.height()); //窗口背景
    //painter.drawLine(-50,-50,ViewWH.width(),-50);       //画X坐标
    //painter.drawLine(-50,-98,ViewWH.width(),-98);       //画X坐标
    //painter.drawLine(-50,_hh,_width,_hh);       //画X坐标
    //painter.drawLine(-50,_hh*2,_width,_hh*2);       //画X坐标
    for(i=0; i<_width; i++)
    {
        //painter.drawLine(0,-pix_size*i,ViewWH.width(),-pix_size*i);       //画X坐标
    }
    //painter.drawLine(0,-50,0,50);           //画Y坐标
    //painter.drawLine(70,-50,70,50);           //画Y坐标
    //painter.drawLine(10,-50,10,30);           //画Y坐标
    for(i=0; i<_height; i++)
    {
        //painter.drawLine(pix_size*i,-ViewWH.height(),pix_size*i,ViewWH.height());           //画Y坐标
    }
    for(int index=0; index<_width; index++)
    {
        for(int i=0; i<_height+1; i++)
        {
            uint32_t color = ugui_get_point(index, i);
            if(color) // if(OLED_GetPoint(index, i))
            {
                painter.setBrush(QColor((color>>16)&0xFF, (color>>8)&0xFF, (color>>0)&0xFF));
                //qDebug("[%s--%d] (%2d,%2d):%d \r\n", __func__, __LINE__, index, i, OLED_GetPoint(index, i));
                //hh=(_height*i)/num;
                //painter.drawLine(hh*index,-hh*i,hh*(index+1),-hh*(i+1));       //画X坐标
                //painter.drawRect(hh*index,-hh*i,hh*(index+1),-hh*(i+1));
                //painter.drawRect(hh*index, -hh*i, hh, hh);
                //painter.drawRect(hh*(_height*index)/num, -hh*(_height*i)/num, hh, hh);
                painter.drawRect(pix_size*index, -pix_size*i, pix_size, pix_size);
            }
        }
    }
}

void FormLCD::gridding(const int _width, const int _height, uint16_t row, uint16_t col)
{
    if(row<1) row=1;
    if(col<1) col=1;
    const int _ww = _width/col;
    const int _hh = _height/row;
    qDebug("[%s--%d] _width:[%d %d %d] _high:[%d %d %d] \r\n", __func__, __LINE__, _width, _ww, col, _height, _hh, row);
    int i;
    QPainter painter(this);
    //painter.setPen(QPen(QColor(11,67,127),8));
    painter.setPen(QPen(QColor(255,255,255),8));
    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿
    //QSize ViewWH(100,100);        //定义视口宽高
    QSize ViewWH(_width, _height);        //定义视口宽高
    painter.setViewport(0, 0,ViewWH.width(),ViewWH.height());
    //painter.setWindow(-50,50,ViewWH.width(),-ViewWH.height());
    painter.setWindow(-50,0,ViewWH.width(),-ViewWH.height());
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),2));
    painter.drawRect(-50,0,ViewWH.width(),-ViewWH.height()); //窗口背景
    //painter.drawLine(-50,-50,ViewWH.width(),-50);       //画X坐标
    //painter.drawLine(-50,-98,ViewWH.width(),-98);       //画X坐标
    //painter.drawLine(-50,_hh,_width,_hh);       //画X坐标
    //painter.drawLine(-50,_hh*2,_width,_hh*2);       //画X坐标
    for(i=0; i<row; i++)
    {
        painter.drawLine(-50,-_hh*i,_width,-_hh*i);       //画X坐标
    }
    //painter.drawLine(0,-50,0,50);           //画Y坐标
    //painter.drawLine(70,-50,70,50);           //画Y坐标
    //painter.drawLine(10,-50,10,30);           //画Y坐标
    for(i=0; i<(col-1); i++)
    {
        painter.drawLine(_ww*i,-ViewWH.height(),_ww*i,ViewWH.height());           //画Y坐标
    }
}

void FormLCD::draw_square(const int _width, const int _height, uint16_t num, uint16_t divide)
{
    uint32_t color, color_tmp;
    if(num<1) num=1;
    const int _ww = _width/num;
    const int _hh = _height/num;
    uint16_t hh=0;
    uint16_t cnt;
    //timeout();
    qDebug("[%s--%d] _width:[%d %d %d] _high:[%d %d %d] \r\n", __func__, __LINE__, _width, _ww, num, _height, _hh, num);
    int i, index;
    QPainter painter(this);
    painter.setPen(QPen(QColor(11,67,127),1));
    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿
    //QSize ViewWH(100,100);        //定义视口宽高
    QSize ViewWH(_height, _height);        //定义视口宽高
    painter.setViewport(0, 0,ViewWH.width(),ViewWH.height());
    //painter.setWindow(-50,50,ViewWH.width(),-ViewWH.height());
    painter.setWindow(-(_width/2-(_height/2)),0,ViewWH.width(),-ViewWH.height());
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),1));
    //painter.drawRect(-50,0,ViewWH.width(),-ViewWH.height()); //窗口背景
    //painter.drawRect(0,0,ViewWH.width(),-ViewWH.height()); //窗口背景
    painter.drawRect(_height/2-_hh*10, -_height/2+_hh*2+10,_hh*21,-_hh*6); //窗口背景
    //painter.drawLine(-50,-50,ViewWH.width(),-50);       //画X坐标
    //painter.drawLine(-50,-98,ViewWH.width(),-98);       //画X坐标
    //painter.drawLine(-50,_hh,_width,_hh);       //画X坐标
    //painter.drawLine(-50,_hh*2,_width,_hh*2);       //画X坐标
    for(i=0; i<num+1; i++)
    {
        hh=(_height*i)/num;
        painter.drawLine(0,-hh,_height,-hh);       //画X坐标
    }
    //painter.drawLine(0,-50,0,50);           //画Y坐标
    //painter.drawLine(70,-50,70,50);           //画Y坐标
    //painter.drawLine(10,-50,10,30);           //画Y坐标
    for(i=0; i<num+1; i++)
    {
        hh=(_height*i)/num;
        painter.drawLine(hh,-ViewWH.height(),hh,ViewWH.height());           //画Y坐标
    }
    painter.setPen(QPen(QColor(255, 0, 0), 3));
    //painter.drawLine(0, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, 0, _height, -_height);           //画Y坐标
    painter.drawLine(0, -_height, _height, 0);           //画Y坐标
    // 绘制延长线
    painter.setPen(QPen(QColor(0, 255, 64), 5));
    const float pi = 3.1415926;
    const float circum = 3.1415926f*2;
    //const float Degree = 2*pi/divide + 0.01745f;   // pi/180=0.01745
    const float Degree = 2*pi/divide + 0.01f;   // pi/180=0.01745
    float degree;
    float angle = 2*pi/divide;
    float radii = ViewWH.height()/2;
    int Radii = ViewWH.height()/2;

    float py=0.0f;
    float px=0.0f;
    float ph=0.0f;
    float pw=0.0f;
#if 1
    hh=(_height*1)/num;
    qDebug("\r\n[%s--%d] bar_len:%d ", __func__, __LINE__, bar_len);
    for(degree=0.0f; degree<Degree; degree +=0.01745f )
    {
        cnt=0;//Bar_Cnt;
        for(i=0; i<divide; i++)
        {
            // 根据三角函数计算出以圆心为原点的等分圆的x,y坐标
            angle = (circum*i)/divide+degree;
            //qDebug("[%s--%d] divide:%d angle:%f circum:%f", __func__, __LINE__, divide, angle, circum);
            py = qSin(angle)*radii;
            px = qCos(angle)*radii;
            // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
            ph = (py*radii)/px;
            // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
            pw = (px*radii)/py;
            // -45°~+45°, <45 || > 315
            if(((circum-pi/4)<angle) || (angle<(pi/4)))
            {
                painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
                UG_DrawLine(_height/hh, (Radii-(int)ph)/hh, Radii/hh, Radii/hh, bar[cnt]);
                //OLED_DrawLine(_height/hh+1, (Radii-(int)ph)/hh, Radii/hh-1, Radii/hh+1, 1);
                //if((circum-pi/4)<angle)
                //{
                    //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, _height, -(Radii-(int)ph), Radii, -Radii);
                    //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, _height/hh+1, (Radii-(int)ph)/hh, Radii/hh-1, Radii/hh+1);
                //}
            }
            // +45°~+135°, >45 && < 315
            // NO.3 --------------
            if(((pi/4)<=angle) && (angle<(3*pi/4)))
            {
                painter.drawLine(Radii+(int)pw, 0, Radii, -Radii);           //画Y坐标
                UG_DrawLine((Radii+(int)pw)/hh, 0, Radii/hh, Radii/hh, bar[cnt]);
            }
            // +135°~+225°, >135 && < 225
            // NO.7 -------------
            if(((3*pi/4)<=angle) && (angle<(5*pi/4)))
            {
                painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
                UG_DrawLine(0, (Radii+(int)ph)/hh, Radii/hh, Radii/hh, bar[cnt]);
                //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, 0, (Radii+(int)ph)/hh, Radii/hh+1, Radii/hh);
            }
            // +225°~+315°, >225 && < 315
            // NO.12 ------------------------
            if(((5*pi/4)<=angle) && (angle<(7*pi/4)))
            {
                painter.drawLine(Radii-(int)pw, -_height, Radii, -Radii);           //画Y坐标
                UG_DrawLine((Radii-(int)pw)/hh, _height/hh, Radii/hh, Radii/hh, bar[cnt]);
                //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, (Radii-(int)pw)/hh, _height/hh+1, Radii/hh, Radii/hh);
            }
            cnt++;
            if(cnt>=bar_len) cnt=0;
            if(cnt<0) cnt=bar_len-1;
            //qDebug("[%s--%d] Radii:%d angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, Radii, angle, px, py, ph, -(Radii-(int)ph), -(Radii-(int)py));
        }
    }
#else
    // NO.0
    // 根据三角函数计算出以圆心为原点的等分圆的x,y坐标
    angle = (circum*0)/divide;
    qDebug("[%s--%d] divide:%d angle:%f circum:%f", __func__, __LINE__, divide, angle, circum);
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
    qDebug("[%s--%d] Radii:%d angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, Radii, angle, px, py, ph, -(Radii-(int)ph), -(Radii-(int)py));
    // NO.1
    // 根据三角函数计算出以圆心为原点的等分圆的x,y坐标
    angle = (circum*1)/divide;
    qDebug("[%s--%d] divide:%d angle:%f circum:%f", __func__, __LINE__, divide, angle, circum);
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
    //painter.drawLine(_height, -(_height/2-(int)py), (_height/2), -(_height/2));           //画Y坐标
    qDebug("[%s--%d] Radii:%d angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, Radii, angle, px, py, ph, -(Radii-(int)ph), -(Radii-(int)py));
    // NO.2
    angle = (circum*2)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    //painter.drawLine(_height, -(int)ph, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
    //painter.drawLine(_height, -(_height/2-(int)py), (_height/2), -(_height/2));           //画Y坐标
    qDebug("[%s--%d] angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, angle, px, py, ph, -(_height/2-(int)ph), -(_height/2-(int)py));
    // NO.3 --------------
    angle = (circum*3)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii+(int)pw, 0, Radii, -Radii);           //画Y坐标
    qDebug("[%s--%d] angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, angle, px, py, ph, -(_height/2-(int)ph), -(_height/2-(int)py));
    // NO.4
    angle = (circum*4)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii+(int)pw, 0, Radii, -Radii);           //画Y坐标
    // NO.5
    angle = (circum*5)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii+(int)pw, 0, Radii, -Radii);           //画Y坐标
    // NO.6
    angle = (circum*6)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii+(int)pw, 0, Radii, -Radii);           //画Y坐标
    // NO.7 -------------
    angle = (circum*7)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    //painter.drawLine(_height, -(int)ph, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
    qDebug("[%s--%d] angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, angle, px, py, ph, -(_height/2-(int)ph), -(_height/2-(int)py));
    // NO.8
    angle = (circum*8)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    //painter.drawLine(_height, -(int)ph, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
    // NO.9
    angle = (circum*9)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    //painter.drawLine(_height, -(int)ph, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
    // NO.10
    angle = (circum*10)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    //painter.drawLine(_height, -(int)ph, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
    // NO.11
    angle = (circum*11)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    //painter.drawLine(_height, -(int)ph, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
    // NO.12 ------------------------
    angle = (circum*12)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii-(int)pw, -_height, Radii, -Radii);           //画Y坐标
    // NO.13
    angle = (circum*13)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii-(int)pw, -_height, Radii, -Radii);           //画Y坐标
    // NO.14
    angle = (circum*14)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii-(int)pw, -_height, Radii, -Radii);           //画Y坐标
    // NO.15
    angle = (circum*15)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii-(int)pw, -_height, Radii, -Radii);           //画Y坐标
    // NO.16
    angle = (circum*16)/divide;
    qDebug("[%s--%d] divide:%d angle:%f circum:%f", __func__, __LINE__, divide, angle, circum);
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
    // NO.17
    angle = (circum*17)/divide;
    qDebug("[%s--%d] divide:%d angle:%f circum:%f", __func__, __LINE__, divide, angle, circum);
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
#endif
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),1));
    //painter.setPen(Qt::red); // setPen 设置轮廓线的颜色
    painter.setPen(QPen(Qt::green, 1));
    painter.setBrush(Qt::blue); // brush是画刷，是填充颜色
    //painter.drawRect(10, -10, 100, -400);
    //painter.drawEllipse(50, -150, 400, -200);
    /*for(i=0; i<num+1; i++)
    {
        hh=(_height*i)/num;
        painter.drawLine(0,-hh,_height,-hh);       //画X坐标
    }*/
    //OLED_DrawLine(0, 0, 10, 10, 1);
    //OLED_DrawLine(43, 1, 20, 22, 1);
    //OLED_DrawLine(41, 20, 20, 22, 1);
    //UG_DrawLine( 41, 41, 21, 21, 1);
    //oled_print(0, 32, 8, 1, "Watting...");
    hh=(_height*1)/num;
    //painter.drawRect(hh, -hh, hh, -hh);
    hh=(_height*1)/num;
    cnt=0;
    color = bar[cnt];
//    for(index=0; index<num; index++)
//    {
//        color = bar[cnt];
//        color_tmp = lcd_get_point(0, index);
//        if(0!=color_tmp)
//        {
//            cnt++;
//            if(cnt>=bar_len) cnt=0;
//            if(cnt<0) cnt=bar_len-1;
//            qDebug("[%s--%d] cnt:%d ", __func__, __LINE__, cnt);
//            continue;
//        }
//        for(i=21; i<num; i++)
//        {
//            color_tmp = lcd_get_point(index, i);
//            qDebug("[%s--%d] (%2d %2d) color_tmp:%06X ", __func__, __LINE__, index, i, color_tmp&0x00FFFFFF);
//            if(0==color_tmp)
//            {
//                lcd_pset(index, i, color);
//            }
//            else break;
//        }
//    }
    //lcd_pset(0, 1, color);
    for(index=0; index<num; index++)
    {
        for(i=0; i<num+1; i++)
        {
            color = ugui_get_point(index, i);
            if(color) // if(OLED_GetPoint(index, i))
            {
                painter.setBrush(QColor((color>>16)&0xFF, (color>>8)&0xFF, (color>>0)&0xFF));
                //qDebug("[%s--%d] (%2d,%2d):%d \r\n", __func__, __LINE__, index, i, OLED_GetPoint(index, i));
                //hh=(_height*i)/num;
                //painter.drawLine(hh*index,-hh*i,hh*(index+1),-hh*(i+1));       //画X坐标
                //painter.drawRect(hh*index,-hh*i,hh*(index+1),-hh*(i+1));
                //painter.drawRect(hh*index, -hh*i, hh, hh);
                //painter.drawRect(hh*(_height*index)/num, -hh*(_height*i)/num, hh, hh);
                painter.drawRect((_height*index)/num, -(_height*i)/num, hh, hh);
            }
        }
    }
}

void FormLCD::draw_circle(const int _width, const int _height, uint16_t divide)
{
#if 0
    int hh = height();//-48;
    uint16_t angle = 30;
    QRectF rectangle(width()/2-(hh/2)-16, height()/2-(hh/2)+32, hh, hh); //位置矩形
#else
    uint16_t angle = 30;
    //QRectF rectangle(_width/2-(_height/2)-16, _height/2-(_height/2)-32, _height, _height); //位置矩形
    QRectF rectangle(_width/2-(_height/2), _height/2-(_height/2), _height, _height); //位置矩形
#endif
//    int startAngle = 30 * 16;     //起始角度,角度被分成了十六分之一
//    int spanAngle = 120 * 16;   //跨越度数
//    QPainter painter(this);painter.drawArc(rectangle, startAngle, spanAngle);
//    QRectF rectangle(10.0, 20.0, 80.0, 60.0);
//    int startAngle = 30 * 16;
//    int spanAngle = 30 * 16;

    QPainter painter(this);
    painter.setPen(QPen(QColor(11,67,127),3));
    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿
//    painter.drawPie(rectangle,30*16,120*16); //绘画角度为30°~(30+120°)
//    painter.drawPie(rectangle,150*16,120*16); //绘画角度为30°~(30+120°)
//    painter.drawPie(rectangle,270*16,120*16); //绘画角度为30°~(30+120°)
    painter.drawPie(rectangle,0*16,120*16); //绘画角度为30°~(30+120°)
    painter.drawPie(rectangle,120*16,120*16); //绘画角度为30°~(30+120°)
    painter.drawPie(rectangle,240*16,120*16); //绘画角度为30°~(30+120°)
    //painter.drawPie(rectangle, startAngle, spanAngle);
    painter.setPen(QPen(QColor(128,128,0),2));
    //painter.drawPie(rectangle,0*16,30*16);
    if(divide>360) divide=360;
    if(divide<1)   divide=1;
    angle = 360/divide;
    for(int i=0; i<divide; i++)
    {
        //painter.drawPie(rectangle,0*16,(30*(i+1))*16);
        painter.drawPie(rectangle,0*16,(angle*(i+1))*16);
    }
    //painter.setPen(QPen(QColor(128,0,0),8));
    //painter.drawPie(rectangle,0*16,(angle*(1+1))*16);
}

void FormLCD::QPaint_Test()
{
    //示例1-设置原点(0,0)为窗口的(100,100),设置绘画区域为窗口大小为100*100
    QPainter painter(this);

    QSize ViewWH(100,100);        //定义视口宽高

    painter.setViewport(50,50,ViewWH.width(),ViewWH.height());

    painter.setWindow(-50,50,ViewWH.width(),-ViewWH.height());

    painter.setBrush(QColor(187,229,253));

    painter.setPen(QPen(QColor(11,67,127),2));

    painter.drawRect(-50,50,ViewWH.width(),-ViewWH.height()); //窗口背景

    painter.drawLine(-50,0,50,0);       //画X坐标

    painter.drawLine(0,-50,0,50);           //画Y坐标

    // 示例2-在窗口的中心处绘制正弦波
    //QPainter painter(this);

    painter.setViewport(50,50,width()-100,height()-100);    //设置视口为中心处

    painter.setWindow(-10,4,20,-8);
                    //定义窗口 左上角为(-10,4) 右下角为(10,-4)

    painter.fillRect(-10,4,20,-8,Qt::black);

    painter.setPen(QPen(QBrush(Qt::green),1/(width()-100)/20));
          //由于当前窗口和视口的比例为 width()-100 : 20
          //所以1px的横纵Line线宽 = 1/(width()-100)/20

    painter.drawLine(-10,0,10,0);

    painter.setPen(QPen(QBrush(Qt::green),1/(height()-100)/8));

    painter.drawLine(0,-4,0,4);

    for(float x=-10;x<10;x+=0.01)
    {
            float y= qSin(x);
            painter.drawPoint(QPointF(x,y));
            qDebug()<<x <<","<<y;
    }
    // Qpainter通过drawText来绘制文本
    //QPainter painter(this);
    QFont font;
    font.setPixelSize(24); //设置字体像素大小为12
    QString text="1234567"; //设置显示的文本
    QFontMetrics metrics(font);
    int w=32;//metrics.width(text); //获取显示文本的宽度
    int h=metrics.height(); //获取显示文本的高度
    painter.setFont(font);
    painter.setPen(Qt::blue);
    painter.drawText(QRect(width()/2-w,height()/2-h,w,h),text);
    painter.drawRect(QRect(width()/2-w,height()/2-h,w,h));
    qDebug()<<"w:"<<w; //一个字符大小为12*24,所以宽度为12*7
    qDebug()<<"h:"<<h; //高度为24
}

void FormLCD::simulation(uint16_t row, uint16_t col)
{
    // 窗口大小
    const int _width=width();
    const int _height=height();
    uint16_t divide = 24;

    uint32_t color, color_tmp;
    if(row<1) row=1;
    if(col<1) col=1;
//    const int _ww = (_width+(col/2))/col;
//    const int _hh = (_height+(row/2))/row;
    uint16_t hh=0;
    uint16_t ww=0;
    uint16_t cnt;
    //qDebug("[%s--%d] _width:[%d %d %d] _high:[%d %d %d] \r\n", __func__, __LINE__, _width, _ww, col, _height, _hh, row);
    int i, index;
    QPainter painter(this);
    painter.setPen(QPen(QColor(11,67,127),1));
    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿
    QSize ViewWH(_height, _height);        //定义视口宽高
    painter.setViewport(0, 0,ViewWH.width(),ViewWH.height());
    painter.setWindow(-(_width/2-(_height/2)),0,ViewWH.width(),-ViewWH.height());
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),1));
    //painter.drawRect(_height/2-_hh*11.5, -_width/2+_ww*2+10,_ww*21,-_hh*6); //窗口背景
    // 画键盘区域
    hh=(ViewWH.height()+(row/2))/row;
    ww=(ViewWH.width()+(col/2))/col;
    painter.drawRect(ViewWH.width()/2-ww*10.5, -ViewWH.height()/2+hh*2,ww*21,-hh*6); //窗口背景
    for(i=0; i<row+1; i++)
    {
        //hh=(_height*i+(row/2))/row;
        //painter.drawLine(0,-hh,_height,-hh);       //画X坐标
        hh=(ViewWH.height()*i+(row/2))/row;
        painter.drawLine(0,-hh,ViewWH.width(),-hh);       //画X坐标
    }
    for(i=0; i<col+1; i++)
    {
        ww=(ViewWH.width()*i+(col/2))/col;
        painter.drawLine(ww,-ViewWH.height(),ww,ViewWH.height());           //画Y坐标
    }
    //qDebug("[%s--%d] \r\n\r\n", __func__, __LINE__);
    painter.setPen(QPen(QColor(255, 0, 0), 3));
    //painter.drawLine(0, 0, (_height/2), -(_height/2));           //画Y坐标
    //painter.drawLine(0, 0, _height, -_height);           //画Y坐标
    //painter.drawLine(0, -_height, _height, 0);           //画Y坐标
    painter.drawLine(0, 0, ViewWH.width(), -ViewWH.height());           //画Y坐标
    painter.drawLine(0, -ViewWH.height(), ViewWH.width(), 0);           //画Y坐标
    // 绘制延长线
    painter.setPen(QPen(QColor(0, 64, 32), 0));
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),1));
    //painter.setPen(Qt::red); // setPen 设置轮廓线的颜色
    painter.setPen(QPen(Qt::green, 1));
    painter.setBrush(Qt::blue); // brush是画刷，是填充颜色
    //hh=(_height*1)/row;
    hh=(ViewWH.height()+(row/2))/row;
    ww=(ViewWH.width()+(col/2))/col;
    cnt=0;
    color = bar[cnt];
    //lcd_pset(0, 1, color);
    //qDebug("\r\n\r\n[%s--%d] hh:%3d ww:%3d", __func__, __LINE__, hh, ww);
    for(index=0; index<col; index++)
    {
        int x;
        int y;
        for(i=0; i<row; i++)
        {
            //color = ugui_get_point(index, i);
            color = Controller.GetLED(index, i);
            if(color) // if(OLED_GetPoint(index, i))
            {
                painter.setBrush(QColor((color>>16)&0xFF, (color>>8)&0xFF, (color>>0)&0xFF));
                //qDebug("[%s--%d] (%2d,%2d):%d \r\n", __func__, __LINE__, index, i, OLED_GetPoint(index, i));
                //hh=(_height*i)/num;
                //painter.drawLine(hh*index,-hh*i,hh*(index+1),-hh*(i+1));       //画X坐标
                //painter.drawRect(hh*index,-hh*i,hh*(index+1),-hh*(i+1));
                //painter.drawRect(hh*index, -hh*i, hh, hh);
                //painter.drawRect(hh*(_height*index)/num, -hh*(_height*i)/num, hh, hh);
                x = (ViewWH.width()*index+(col/2))/col;
                y = (ViewWH.height()*i+(row/2))/row;
                painter.drawRect(x, -y, ww, -hh);
                //qDebug("[%s--%d] (%2d, %2d) [%3d, %3d] (%3d, %3d)", __func__, __LINE__, index, i, ViewWH.width(), ViewWH.height(), x, y);
            }
        }
        //if(index>=39) break;
    }
    // 画键盘区域
    int x1,y1;
    int x2,y2;
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),5));
    hh=(ViewWH.height()+(row/2))/row;
    ww=(ViewWH.width()+(col/2))/col;
    x1 = ViewWH.width()/2-ww*10.7;
    y1 = -ViewWH.height()/2+hh*2.5;
    x2=x1+ww*21.4;
    y2=y1-hh*6;
    painter.drawLine(x1, y1, x1, y2);           //上
    painter.drawLine(x1, y2, x2, y2);           //下
    painter.drawLine(x2, y2, x2, y1);           //左
    painter.drawLine(x2, y1, x1, y1);           //右
//    painter.drawRect(ViewWH.width()/2-ww*10.5, -ViewWH.height()/2+hh*2,ww*21,-hh*6); //窗口背景
}
